/**
 * 整体思路与二叉树层序遍历一致，只是子节点进队列式需要使用for of循环
 * @function {queue}
*/

var levelOrder = root => {
  const res = [],
    queue = []
  if (!root) return res
  queue.push(root)

  while (queue.length) {
    const cur = []
    let len = queue.length
    for (let i = 0; i < len; i++) {
      const node = queue.shift()
      cur.push(node.val)

      for (const child of node.children) {
        child && queue.push(child)
      }
    }
    res.push(cur)
  }
  return res
}


console.log(levelOrder(root))